package com.shengqi.xinnongren.work.ui.user.fragments;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpFragment;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.common.widgets.CustomDialog;
import com.shengqi.xinnongren.work.MyApplication;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.activities.MainActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyCouponActivity;
import com.shengqi.xinnongren.work.ui.user.model.CouponBean;
import com.shengqi.xinnongren.work.ui.user.presenter.CouponPresenter;
import com.shengqi.xinnongren.work.ui.user.view.CouponView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by LuoYaWei on 2018/6/27.
 */
public class CouponFragment extends BaseMvpFragment<CouponView, CouponPresenter> implements CouponView, SwipeMenuCreator, SwipeMenuItemClickListener {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_coupon)
    SwipeMenuRecyclerView mRecyclerView;
    @BindView(R.id.ll_no_coupon)
    LinearLayout ll_no_coupon;

    private CouponAdapter mAdapter;
    private List<CouponBean> mList;
    private String mType; //	1可用,2已用,3已过期
    private int pageIndex = 0;

    @Override
    protected void initView() {

        Bundle arguments = getArguments();
        if (arguments != null) {
            mType = arguments.getString("type");
        }

        mList = new ArrayList<>();

        mRecyclerView.setSwipeMenuCreator(this);
        mRecyclerView.setSwipeMenuItemClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(Global.genRecyclerViewItemDecoration(getActivity(), 16, ContextCompat.getColor(getActivity(), R.color.colorMainBg)));
        mAdapter = new CouponAdapter(R.layout.item_coupon, mList);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.item_use:
                    startActivity(new Intent(getActivity(), MainActivity.class));
                    LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(new Intent(MainActivity.BROADCAST_CHECK_CLASS));
                    break;
            }
        });
    }

    @Override
    protected void initData() {
        mRefreshLayout.setOnRefreshListener(refreshLayout -> getData());
    }

    private void getData() {
        mList.clear();
        pageIndex = 0;
        getPresenter().getCouponList(pageIndex, mType);
    }

    @Override
    protected void lazyLoadData() {
        getPresenter().getCouponList(pageIndex, mType);
    }

    @Override
    protected CouponPresenter createPresenter() {
        return new CouponPresenter();
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_coupon;
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
        int width = (int) getResources().getDimension(R.dimen.dp_70);
        int height = ViewGroup.LayoutParams.MATCH_PARENT;

        SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                .setBackgroundColorResource(R.color.colorPrimary)
                .setText("删除")
                .setTextSize(18)
                .setTextColor(Color.WHITE)
                .setWidth(width)
                .setHeight(height);
        swipeRightMenu.addMenuItem(deleteItem);
    }

    @Override
    public void onItemClick(SwipeMenuBridge menuBridge) {
        int position = menuBridge.getAdapterPosition();
        new CustomDialog.Builder(getContext())
                .setTitle("删除优惠券")
                .setContent("确认删除优惠券：" + mList.get(position).getName())
                .setListener(new CustomDialog.OnButtonClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        getPresenter().deleteCoupon(mList.get(position).couponId);
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                }).build().show();

        menuBridge.closeMenu();
    }

    @Override
    public void showCouponList(List<CouponBean> list) {
        mRefreshLayout.finishRefresh();
        mList.clear();
        mList.addAll(list);
        ll_no_coupon.setVisibility(mList.size() > 0 ? View.GONE : View.VISIBLE);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDelCouponRes(String message) {
        ToastHelper.getInstance(getContext()).showSucceed(message);
        getData();
        ((MyCouponActivity) getActivity()).updateCouponNum();
    }

    class CouponAdapter extends BaseQuickAdapter<CouponBean, BaseViewHolder> {

        public CouponAdapter(int layoutResId, @Nullable List<CouponBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CouponBean item) {
            helper.setText(R.id.item_money, item.discountAmount + "");
            helper.setText(R.id.item_use_amount, "满" + item.useAmount + "元可用");
            helper.setText(R.id.item_title, item.name);
            helper.setText(R.id.item_time, "有效期：" + item.time);
            //	1可用,2已用,3已过期
            helper.setImageResource(R.id.item_pic, mType.equals("1") ? R.mipmap.ic_coupon_unused : R.mipmap.ic_coupon_used);
            helper.setVisible(R.id.item_use, mType.equals("1"));
            helper.setVisible(R.id.item_had_use, !mType.equals("1"));

            helper.setImageResource(R.id.item_had_use, mType.equals("2") ? R.mipmap.ic_coupon_used_l : R.mipmap.ic_coupon_expired);

            helper.addOnClickListener(R.id.item_use);
        }
    }

}
